/********************************************************************************
 *  
 *  Product: GSAOBKeyMatchesLib
 *  Description: A C# API for Responsing to Google Search Appliance OneBoxModule.
 *
 *  (c) Copyright 2007 
 *  
********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;

namespace GSAOBKeyMatchesLib.Client
{
    /// <summary>
    /// Provides methods to create Phrases  objects wich used in KeyMatch Object with "Phrase Keymatch" matchtype
    /// <para>Author Albert Ghukasyan</para>
    /// </summary>
    class Phrases
    {
        #region Variables

        public String[] words;
        public int i = 0;
        public int j = 0;
        public bool hasMore = true;

        #endregion

        #region Constructor

        public Phrases(String canonicalQuery)
        {
            this.words = canonicalQuery.Split(' ');
        }

        #endregion

        #region Methods

        public bool hasMoreElements()
        {
            return hasMore;
        }

        public Object nextElement()
        {
            if (!hasMore)
            {
                return null;
            }
            StringBuilder next = new StringBuilder();
            for (int k = 0; k <= j; k++)
            {
                if (k != 0)
                {
                    next.Append(" ");
                }
                next.Append(words[i + k]);
            }
            // System.out.println(next.toString());
            moveIndex();
            return next.ToString();
        }

        void moveIndex()
        {
            // System.out.println("Calling moveIndex with i = " + i + " and j = " + j);
            if (i <= words.Length - 1 && i + j <= words.Length - 2)
            {
                j++;
            }
            else if (i <= words.Length - 2)
            {
                i++;
                j = 0;
            }
            else
            {
                hasMore = false;
            }
        }

        #endregion
    }
}
